<?php

/***  DOCUMENTATION LAYER

Last Update: Jun 2007
	
DESCRIPTION
	test Analytics cron script
	
	designed to allow testing in browser.  once it has been tested, move to the
	$safe_dir_name, where it will only be accessible by a local script.  not the
	supermax secure, but good enough in most cases
	
	some possible settings:
		wget -O /dev/null 'http://www.domain.com/test.cron.php?cron=cron'
		lynx -dump 'http://www.domain.com/test.cron.php?cron=cron'
		
	others
		/usr/bin/php -q /usr/home/USERNAME/test.cron.php?cron=cron
		php -q /usr/home/USERNAME/test.cron.php?cron=cron		* GET variables not recognized
		GET http://domain.com/script.php > /dev/null				* Not available on many servers		
	
***/

// Kill Switch
if ( 0 ) die('<h2>this script has been disabled</h2><a href="http://www.klenwell.net/is/HomePage">klenwell cron jobs</a>');

// Cron Guard
$safe_dir_name = '_cron_guarded';
if ( strpos($_SERVER['PHP_SELF'], $safe_dir_name) && $_SERVER['REMOTE_ADDR'] !== $_SERVER['SERVER_ADDR'] )
	die('<a href="http://www.klenwell.net/is/HomePage">klenwell cron jobs</a><!-- local cron access only -->');

// localhost?
$_FLAG['is_localhost'] = ( $_SERVER['HTTP_HOST'] == 'localhost' ) ? 1 : 0;


// Load Files
$_DIR['greqo'] = ( $_FLAG['is_localhost'] ) ? '../__SUPER/GREQO/' : dirname($_SERVER['DOCUMENT_ROOT']) . '/GREQO/'; 
require_once($_DIR['greqo'] . 'analytics.class.php');
require_once($_DIR['greqo'] . 'gmail.class.php');
require_once($_DIR['greqo'] . 'test/login.inc.php');

// Load Greqo Object
$GreqoAnalytics = new GreqoAnalytics($debug=0);
$GreqoAnalytics->print_d('cron testing : don\'t forget to GET the cron');


// Get-Activated Portion of Script
if ( $_GET['cron'] == 'cron' )
{
	// access settings
	$GreqoAnalytics->email = $_GRQ['email'];
	$GreqoAnalytics->password = $_GRQ['password'];
	$GreqoAnalytics->site_id = $_GRQ['site_id'];
	$GreqoAnalytics->site_id = '2752623';
	
	// get report
	$start_uts = time() - ($GreqoAnalytics->day_sec * 2);
	$stop_uts = time() - $GreqoAnalytics->day_sec;
	if ( !$REPORT_SUMMARY = $GreqoAnalytics->get_site_summary($start_uts, $stop_uts, $site_id=0) )
	{
		die('unable to get report');
	}
	$GreqoAnalytics->print_d("Report prepared for {$REPORT_SUMMARY['META']['profile_name']}", 'green');
	
	// prepare email body
	$report_dump = print_r($REPORT_SUMMARY, 1);
	$mail_body = <<<TEXT
	
Greqo Analytics Summary for {$REPORT_SUMMARY['META']['profile_name']}

DATES: {$REPORT_SUMMARY['META']['date_range']}
TOTAL VISITORS: {$REPORT_SUMMARY['VISITORS']['visits']}
UNIQUE VISITORS: {$REPORT_SUMMARY['VISITORS']['unique_visits']}
TOTAL PAGE VIEWS: {$REPORT_SUMMARY['VISITORS']['pageviews']}

{$report_dump}
	
TEXT;

	// output email body
	echo "<pre>{$mail_body}</pre>";	
	
	// prepare gmailer
	$mail_to = $_GRQ['email'];
	$Mailer = new GmailMailer($debug=0);
	$Mailer->Username = $_GRQ['email'];
	$Mailer->Password = $_GRQ['password'];
	$Mailer->FromName = 'Greqo Mailer';
	$Mailer->AddAddress($mail_to, 'Analytics User');
	$Mailer->Subject = 'Greqo Analytics Report: ' . "{$REPORT_SUMMARY['META']['profile_name']} ({$REPORT_SUMMARY['META']['date_range']})";
	$Mailer->Body = $mail_body;
	
	// localhost : use Gmail SMTP
	if ( $_FLAG['is_localhost'] )
	{
		$Mailer->print_d('using Gmail SMTP');
		if ( !$Mailer->send_email() ) $Mailer->print_d('sorry, unable to deliver email', 'red');
		else $Mailer->print_d("mail sent to $mail_to", 'green');
	}
	// a remote host : use PHPMailer mail method
	else
	{
		$Mailer->print_d('using PHPMailer mail');
		$Mailer->Mailer = 'mail';
		if ( !$Mailer->send() ) $Mailer->print_d('sorry, unable to deliver email', 'red');
		else $Mailer->print_d("mail sent to $mail_to", 'green');
	}
}

?>
